package com.onenine.app.activity;

import android.content.DialogInterface;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.onenine.app.R;
import com.onenine.app.adapter.GreetMsgAdapter;
import com.onenine.app.db.DataManager;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.base.BasePageListActivity;
import com.onenine.base.view.PullRefreshRecyclerView;
import com.onenine.chat.model.LNewFriend;
import com.onenine.chat.rongcloud.RcUser;
import com.onenine.dialog.CommonTextDialog;

/**
 * 创建时间：2018/6/12 0012 12:02
 * 创建人：fly
 * 功能描述：打招呼消息
 */
public class GreetListMsgActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener
        , View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {

    private PullRefreshRecyclerView mRecyclerViewLayout;
    private GreetMsgAdapter mFriendAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_pull_refresh_layout;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mToolbar.setCenterTitle(R.string.greet_message);
        mToolbar.setRightTitle(R.string.clear_all);
        mRecyclerViewLayout = findViewById(R.id.pull_to_refresh_layout);
        mFriendAdapter = new GreetMsgAdapter(DataManager
                .getAllFriendMessageByUserId(RcUser.getInstance().getUserId()));
        if (BasePageListActivity.sColorSchemeResources != null) {
            mRecyclerViewLayout.setColorSchemeColors(BasePageListActivity.sColorSchemeResources);
        }
        mRecyclerViewLayout.setAdapter(mFriendAdapter);
        mRecyclerViewLayout.setEnableLoadMore(false);
        mRecyclerViewLayout.setPullEmptyView();
    }

    @Override
    protected void initListeners() {
        mToolbar.setOnRightClickListener(this);
        mFriendAdapter.setOnItemChildClickListener(this);
        mRecyclerViewLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_right_title:
                if (mFriendAdapter.getItemCount() >= 0) {
                    showDelDialog();
                }
                break;
        }
    }

    /**
     * 清空前确认
     */
    private void showDelDialog() {
        new CommonTextDialog.Builder(this)
                .setText(R.string.delete_all_message)
                .setOnOkClickListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DataManager.deleteAllByUserId(RcUser.getInstance().getUserId());
                        mFriendAdapter.setNewData(DataManager
                                .getAllFriendMessageByUserId(RcUser.getInstance().getUserId()));
                    }
                }).builder().show();
    }

    @Override
    public void onRefresh() {
        mFriendAdapter.setNewData(DataManager
                .getAllFriendMessageByUserId(RcUser.getInstance().getUserId()));
        mRecyclerViewLayout.setRefreshing(false);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        LNewFriend newFriend = (LNewFriend) adapter.getItem(position);
        if (view.getId() == R.id.rl_item) {
            newFriend.setRead(true);
            mFriendAdapter.notifyItemChanged(position);
            DataManager.updateNewFriendMessageReadedByUserId(newFriend.getUserId(), newFriend.getTargetUserId());
            PrivateMessageActivity.launchActivity(GreetListMsgActivity.this
                    , newFriend.getTargetUserId(), ((TextView) adapter.getViewByPosition(position, R.id.tv_name)).getText().toString());
        } else if (view.getId() == R.id.btn_delete) {
            newFriend.delete();
            mFriendAdapter.remove(position);
        }
    }

}
